/********************************************************************
  Mega Mario SRC
    created:	2005/09/18
	created:	18:9:2005   10:13
	author:		Jens Wellmann (c)
*********************************************************************/

#include "Global.h"
#include "turtle.h"
#include "functions.h"
#include "MessageBus.h"
#include "CGamePlayStatics.h"

SDL_Surface * cTurtle::TURTLE_SURFACE[2][2] = {NULL};
SDL_Surface * cTurtle::TURTLEFLY_SURFACE[2][2] = { NULL };
SDL_Surface * cTurtle::TURTLE_SURFACE_SHELL[4] = { NULL };

void cTurtle::initTURTLE()
{
	TURTLEFLY_SURFACE[0][1] = LoadIMG(DATADIR "gfx/characters/turtle/flyl1.png");
	TURTLEFLY_SURFACE[0][0] = LoadIMG(DATADIR "gfx/characters/turtle/flyl2.png");
	TURTLEFLY_SURFACE[1][1] = LoadIMG(DATADIR "gfx/characters/turtle/flyr1.png");
	TURTLEFLY_SURFACE[1][0] = LoadIMG(DATADIR "gfx/characters/turtle/flyr2.png");

	TURTLE_SURFACE[0][0] = LoadIMG(DATADIR "gfx/characters/turtle/turtlel1.png");
	TURTLE_SURFACE[0][1] = LoadIMG(DATADIR "gfx/characters/turtle/turtlel2.png");
	TURTLE_SURFACE[1][0] = LoadIMG(DATADIR "gfx/characters/turtle/turtler1.png");
	TURTLE_SURFACE[1][1] = LoadIMG(DATADIR "gfx/characters/turtle/turtler2.png");

	TURTLE_SURFACE_SHELL[0] = LoadIMG(DATADIR "gfx/characters/turtle/shell1.png");
	TURTLE_SURFACE_SHELL[1] = LoadIMG(DATADIR "gfx/characters/turtle/shell2.png");
	TURTLE_SURFACE_SHELL[2] = LoadIMG(DATADIR "gfx/characters/turtle/shell3.png");
	TURTLE_SURFACE_SHELL[3] = LoadIMG(DATADIR "gfx/characters/turtle/shell4.png");
}

cTurtle::cTurtle(int x,int y) : cEnemy(x,y)
{
	W = cTurtle::TURTLE_SURFACE[0][0]->w - 4;
	H = cTurtle::TURTLE_SURFACE[0][0]->h;
	this->down = 0;
	scoring = 200;
	this->dead = false;
	this->dying = false;
	this->DEATHcount = 0;
}

cTurtle::~cTurtle()
{

}

bool cTurtle::init()
{
	return true;
}

void cTurtle::move(float dt, cPlayer* mario)
{
	/*if (!this->dead && !this->dying && collision(&mario->rect, &this->rect))
	{
		this->die();
	}*/
	this->update();
	if (!this->dead)
	{
		if (this->down && this->rect.x != rect.x && collision(&this->rect, &this->rect))
		{
			dir *= -1;
			x += dir + dir;
		}
	}
	if (dying)
		return;
	if (down)
	{
		if (!dir)
		{
			down--;
			if (1 == down)
			{
				this->H = cTurtle::TURTLE_SURFACE[0][0]->h;
				this->y -= cTurtle::TURTLE_SURFACE[0][0]->h - cTurtle::TURTLE_SURFACE_SHELL[0]->h;
				dir = ENEMYSPEED;
				down = 0;
			}
		}
		else
		{
			W;
		}
	}
	if ((playerTouchLeft(mario)))
	{
		if (down && !dir)
		{
			x -= 25;
			y -= 10;
			dir = -7;
			//SCORE->init(x,y,400);
			MessageBus::get_MsgBusInstance()->sendMessageFunc<void, int, int, int, const char *>
				(x, y, 400, "InitScore");
			PLAYSOUND1(S_KICK);
		}
		else
		{
			if (!mario->stat)
				mario->die();
			else
				mario->changeStat(0);
		}
	}
	else if (playerTouchRight(mario))
	{
		if (down && !dir)
		{
			x += 25;
			y -= 10;
			dir = 7;
			MessageBus::get_MsgBusInstance()->sendMessageFunc<void, int, int, int, const char *>
				(x, y, 400, "InitScore");
			PLAYSOUND1(S_KICK);
		}
		else
		{
			if (!mario->stat)
				mario->die();
			else
				mario->changeStat(0);
		}
	}
	/*for (int i = 0; i < TURTLEcount; i++) {
			if (!TURTLE[i]->dead)
			{
				if (TURTLE[i]->down && TURTLE[i]->rect.x != rect.x && collision(&TURTLE[i]->rect, &this->rect))
				{
					dir *= -1;
					x += dir + dir;
				}
			}
		}*/
}

void cTurtle::itemCollision(cPlayer* mario, std::vector<NpcItem *> &items)
{
	this->turtleSlidder(items);
}

void cTurtle::turtleSlidder(std::vector<NpcItem *> &items)
{
    std::vector<NpcItem *>::iterator npcIt = items.begin();
	for (; npcIt != items.end(); npcIt++) {
		NpcItem *item = *npcIt;
		if (collision(&this->rect, &item->GetItemRect()))
		{
			CDamageType damageType;
			CGamePlayStatics::ApplyDamage(item, 10, this, damageType);
		}
	}
	//collisionWithOthers  判断碰撞伤害
	for (int i = 0; i < GOOMBAcount; i++)
		if (!GOOMBA[i]->DEATHcount)
		{
			if (collision(&GOOMBA[i]->rect, &this->rect))
				GOOMBA[i]->die();
		}
	/*for(int i = 0;i < TURTLEcount;i++)
		if(!TURTLE[i]->dying)
		{
			if(TURTLE[i]->rect.x != rect.x && collision(&TURTLE[i]->rect,&this->rect))
				TURTLE[i]->die();
		}*/
	if (!this->dying)
	{
		//if (this->rect.x != this->rect.x && collision(&this->rect, &this->rect))
			//this->die();
	}
	for (int i = 0; i < TURTLEFLYcount; i++)
		if (!TURTLEFLY[i]->dead && TURTLEFLY[i]->rect.x != this->rect.x && collision(&TURTLEFLY[i]->rect, &this->rect))
			TURTLEFLY[i]->die();

	for (int i = 0; i < SPINYcount; i++)
		if (!SPINY[i]->dying)
		{
			if (collision(&SPINY[i]->rect, &this->rect))
				SPINY[i]->die();
		}
	LEVEL->Bonus_Kick_Mode = 1;//Switch Bonus_Kick_Mode to TURLTE Mode
	if (this->right_touch || this->left_touch)
		LEVEL->BonusCheck(this->x + this->W + 15, this->y + this->H / 1.25, this->x - 15, this->y + this->H / 1.25);
	LEVEL->Bonus_Kick_Mode = 0;//Switch Bonus_Kick_Mode back to Mario Mode
}
    
void cTurtle::headbang()
{
	PLAYER->y_speed = -10;
	PLAYSOUND1(S_STOMP2);
	this->down = 500;
	MessageBus::get_MsgBusInstance()->sendMessageFunc<void, int, int, int, const char *>
		(x, y, 100, "InitScore");
	H = TURTLE_SURFACE_SHELL[0]->h-3;
	if (dir) {
		dir = 0;
	}
	else {
		dir = 7;
	}
}

void cTurtle::draw()
{
	int d = (dir<0) ? 0 : 1;
	if(down)
		SDL_UpperBlit(cTurtle::TURTLE_SURFACE_SHELL[walkcount],0,screen,&rect);
	else 
		SDL_UpperBlit(cTurtle::TURTLE_SURFACE[d][walkcount],0,screen,&rect);
}

cTurtleFly::cTurtleFly(int x,int y) : cEnemy(x,y)
{
	W = cTurtle::TURTLEFLY_SURFACE[0][0]->w-4;
	H = cTurtle::TURTLEFLY_SURFACE[0][0]->h;
	scoring = 200;
	count = 50;
}

void cTurtleFly::move(float dt, cPlayer* mario)
{
	this->update();
	if (dying)
		return;
	if (LEVEL->posValid(x + 3, y) + LEVEL->posValid(x + W - 3, y))
	{
		count = 30;
		down_touch = 0;
	}
	if (count < 15)
	{
		y -= 2;
		y_speed = -4;
		count++;
	}
	if (down_touch)
		count = 0;
	if ((playerTouchLeft(mario) + playerTouchRight(mario)))
		if (!mario->stat)
			mario->die();
		else
			mario->changeStat(0);
}

void cTurtleFly::headbang()
{
	PLAYER->y_speed=-10;
	PLAYSOUND1(S_STOMP2);
	MessageBus::get_MsgBusInstance()->sendMessageFunc<void, int, int, int, const char *>
		(x, y, 200, "InitScore");
	dead  = 1;
	//TURTLE[TURTLEcount]= new cTurtle(x,y);
	//TURTLE[TURTLEcount]->dir = dir;
	//TURTLEcount++;
}

void cTurtleFly::draw()
{
	int d = (dir<0) ? 0 : 1;
	SDL_UpperBlit(cTurtle::TURTLEFLY_SURFACE[d][walkcount],0,screen,&rect);
}